<template>
	<view class="orderGoods" :style="colorStyle">
		<view class='total' v-if="is_behalf"><text>
				{{$t(`代付金额`)}}：
				<text class="pay-price">￥{{pay_price || 0}}</text>
			</text>
		</view>
		<view class='total flex_s' v-else-if="!split && !is_behalf">
			<checkbox-group @change="checkboxAllChange">
				<checkbox value="all" :checked="!!isAllSelect" />
				<text class='checkAll'>{{$t(`全选`)}}</text>
			</checkbox-group>
			<text>{{$t(`共`)}}{{totalNmu}}{{$t(`件商品`)}}</text>
		</view>
		<!-- <view class='total' v-else-if="split">
			<text>{{$t(`订单包裹`)}} {{index + 1}}</text>
			<view class="rig-btn" v-if="status_type === -1">
				<view class="refund">{{$t(`申请退款中`)}}</view>
			</view>
			<view class="rig-btn" v-else-if="status_type === -2">
				<view class="refund">{{$t(`已退款`)}}</view>
			</view>
			<view class="rig-btn" v-else-if="status_type === 4">
				<view class="done">{{$t(`已完成`)}}</view>
			</view>
		</view> -->

		<view class='goodWrapper'>
			<view v-for="(item,index) in cartInfo" :key="index" @click="jumpCon(item.product_id)">
				<view class="item acea-row row-between-wrapper goods_item" style="padding: 10rpx 0;height: auto;">
					<view class="check_box">
						<checkbox-group @change="ChangeGoldHandle(index)">
							<checkbox :value="index" :checked='item.is_trade_in' />
						</checkbox-group>
					</view>
					<view class='pictrue' style="flex-shrink: 0;">
						<image :src='item.productInfo.attrInfo.image' v-if="item.productInfo.attrInfo"></image>
						<image :src='item.productInfo.image' v-else></image>
					</view>
					<view class='text'>
						<view class='acea-row row-between-wrapper' style="align-items: flex-start;">
							<view class='name line1'>{{item.productInfo.store_name}}</view>
							<!-- 							<view class="gold_price" v-if="!item.is_trade_in">
								金价：{{item.gold_price.askprice||0}}
							</view> -->
						</view>
						<!-- <view class='attr line1' v-if="item.productInfo.attrInfo">{{item.productInfo.attrInfo.suk}}
						</view> -->
						<view class='attr line1 flex_s' v-if="item.productInfo.attrInfo" style="margin-top: 15rpx;">
							<view class="price_item flex_s">
								克重:<text>{{item.productInfo.attrInfo.weight||0}}克</text>
								<view class='num' v-if="pageType=='cart'">x {{item.cart_num}}</view>
							</view>
							<view class="price_item"
								v-if="(pageType=='order'||pageType=='confirm')&&Number(item.productInfo.attrInfo.labor_charges)">
								<view v-if="authenticate_type==0">
									工费:<text>{{item.productInfo.attrInfo.labor_charges}}/g</text></view>
								<view v-else>工费:<text>{{item.productInfo.attrInfo.certifi_labor_charges}}/g</text>
								</view>
							</view>
						</view>
						<view class='money font-color pic' v-if="item.productInfo.attrInfo" style="margin-top: 10rpx;">
							<text :class="{gray:!item.is_valid}" style="color: #D29E41;font-size: 30rpx;">
								{{$t(`￥`)}}{{item.productInfo.attrInfo.price}}
							</text>
							<!-- <view class="refund" v-if="item.refund_num && statusType !=-2">{{item.refund_num}}{{$t(`件退款中`)}}
							</view> -->
							<!-- <text class="valid" v-if="!item.is_valid && shipping_type === 0">{{$t(`不支持配送`)}}</text>
							<text class="valid"
								v-if="!item.productInfo.store_mention && shipping_type === 1">{{$t(`不支持自提`)}}</text> -->
						</view>
						<view class='money font-color pic' v-else style="margin-top: 10rpx;">
							<text :class="{gray:!item.is_valid}">{{$t(`￥`)}}{{item.productInfo.price}}</text>
							<text class="valid" v-if="!item.is_valid && shipping_type === 0">{{$t(`仅支持到店`)}}</text>
							<text class="valid"
								v-if="!item.productInfo.store_mention && shipping_type === 1">{{$t(`仅支持配送`)}}</text>
						</view>
						<view class='evaluate' v-else-if="item.is_reply==1">{{$t(`已评价`)}}</view>
					</view>
				</view>
				<!-- <view class="btm_box flex_sb" v-if="pageType=='confirm'&&news==0">
					<view class="count_num">
						<text>数量</text>
					</view>
					<view class="cart_box flex_s">
						<view class="mysicle reduce acea-row row-center-wrapper" :class="item.numSub ? 'on' : ''"
							@click.stop='subCart(index)'>
							<text class="iconfont icon-shangpinshuliang-jian"></text>
						</view>
						<view class=' num acea-row row-middle'>
							<input type="number" v-model="item.cart_num" data-name="productSelect.cart_num"
								@input="iptCartNum(index)" @blur="blurInput(index)"></input>
						</view>

						<view  class=" mysicle plus acea-row row-center-wrapper"
							:class="item.numAdd ? 'on' : ''" @click.stop='addCart(index)'>
							<text class="iconfont icon-shangpinshuliang-jia"></text>
						</view>
					</view>
				</view> -->

				<view class="botton-btn">
					<view class='logistics' v-if="item.is_reply==0 && evaluate==3 && pid != -1 && isShow"
						@click.stop="evaluateTap(item.unique,orderId)">
						{{$t(`评价`)}}
					</view>
					<view class='logistics' v-if="can_exchange" @click.stop="openSubcribe(item)">
						{{$t(`申请换货`)}}
					</view>
					<view class="rig-btn" v-if="status_type === 2 && index === cartInfo.length - 1 || !split">
						<view v-if="delivery_type === 'express'" class="logistics" @click.stop="logistics(orderId)">
							{{$t(`查看物流`)}}
						</view>
						<view class="logistics sure" v-if="status_type === 2" @click.stop="confirmOrder(orderId)">
							{{$t(`确认收货`)}}
						</view>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		getCartList,
		getCartCounts,
		changeCartNum,
		cartDel,
		getResetCart
	} from "@/api/order.js"
	import colors from "@/mixins/color";
	export default {
		props: {
			news: {
				type: Number,
				default: 0,
			},
			// 订单状态
			statusType: {
				type: Number,
				default: 0,
			},
			// 订单状态
			can_exchange: {
				type: Boolean,
				default: false,
			},
			virtualType: {
				type: Number,
				default: 0,
			},
			evaluate: {
				type: Number,
				default: 0,
			},
			oid: {
				type: Number,
				default: 0,
			},
			// 1已支付 0未支付
			paid: {
				type: Number,
				default: 0,
			},
			cartInfo: {
				type: Array,
				default: function() {
					return [];
				}
			},
			orderId: {
				type: String,
				default: '',
			},
			order_status_text: {
				type: String,
				default: '',
			},
			shipping_type: {
				type: Number,
				default: -1,
			},
			delivery_type: {
				type: String,
				default: '',
			},
			pay_price: {
				type: String,
				default: '',
			},
			jump: {
				type: Boolean,
				default: false,
			},
			is_confirm: {
				type: Boolean,
				default: false,
			},
			// is_behalf 是否是代付列表
			is_behalf: {
				type: Boolean,
				default: false,
			},
			split: {
				type: Boolean,
				default: false,
			},
			jumpDetail: {
				type: Boolean,
				default: false,
			},
			index: {
				type: Number,
				default: 0,
			},
			pid: {
				type: Number,
				default: 0,
			},
			refund_status: {
				type: Number,
				default: 0,
			},
			status_type: {
				type: Number,
				default: 0,
			},
			isShow: {
				type: Boolean,
				default: true,
			},
			is_refund_available: {
				type: Boolean,
				default: true,
			},
			pageType: {
				type: String,
				default: 'cart'
			}
		},
		mixins: [colors],
		data() {
			return {
				totalNmu: 0,
				operationModel: false,
				status: "",
				cartList: [],
				isAllSelect: false
			};
		},
		watch: {
			cartInfo: function(nVal, oVal) {
				this.cartList = nVal
				let num = 0
				nVal.forEach((item, index) => {
					num += item.cart_num
				})
				this.totalNmu = num
			}
		},
		mounted() {
			let num = 0
			this.$nextTick(() => {
				this.cartInfo.forEach((item, index) => {
					num += item.cart_num
				})
				this.$set(this, 'totalNmu', num)
			})

		},
		methods: {
			checkboxAllChange: function(event) {
				let value = event.detail.value;
				if (value.length > 0) {
					this.isAllSelect = true
					this.$emit('setAllSelectValue', 1)
				} else {
					this.isAllSelect = false
					this.$emit('setAllSelectValue', 0)
				}
			},
			ChangeGoldHandle(index) {
				this.$emit('ChangeGoldHandle', index)
			},
			changeHandle(name, index) {
				const data = {
					name: name,
					index: index
				}
				this.$emit('changeCart')
			},
			subCart: function(index) {
				let that = this;
				let status = false;
				let item = that.cartInfo[index];
				item.cart_num = Number(item.cart_num) - 1;
				if (item.cart_num < 1) status = true;
				if (item.cart_num <= 1) {
					item.cart_num = 1;
					item.numSub = true;
				} else {
					item.numSub = false;
					item.numAdd = false;
				}
				if (false == status) {
					that.setCartNum(item.id, item.cart_num, function(data) {
						that.cartInfo[index] = item;
						that.getCartNum();
					}, () => {
						item.cart_num = Number(item.cart_num) + 1
					})
				}
			},
			addCart: function(index) {
				let that = this;
				if (this.adding) return
				let item = that.cartInfo[index];
				item.cart_num = Number(item.cart_num) + 1
				let productInfo = item.productInfo;
				if (productInfo.hasOwnProperty('attrInfo') && item.cart_num >= item.productInfo.attrInfo.stock) {
					item.cart_num = item.productInfo.attrInfo.stock;
					item.numAdd = true;
					item.numSub = false;
				} else {
					item.numAdd = false;
					item.numSub = false;
				}
				that.setCartNum(item.id, item.cart_num, (data) => {
					that.cartInfo[index] = item;
					that.getCartNum();
				}, () => {
					item.cart_num = Number(item.cart_num) - 1
				})
			},
			setCartNum(cartId, cartNum, successCallback, errorCallback) {
				let that = this;
				changeCartNum(cartId, cartNum).then(res => {
					successCallback && successCallback(res.data);
				}).catch(err => {
					errorCallback && errorCallback()
					return that.$util.Tips({
						title: err
					});
				})
			},
			getCartNum: function() {
				let that = this;
				getCartCounts().then(res => {
					that.cartCount = res.data.count;
					this.adding = false
					this.$store.commit('indexData/setCartNum', res.data.count > 99 ? '..' : res.data.count)
					this.changeHandle()
					if (res.data.count > 0) {
						wx.setTabBarBadge({
							index: 2,
							text: res.data.count + ''
						})
					} else {
						wx.hideTabBarRedDot({
							index: 2
						})
					}

				});
			},
			evaluateTap: function(unique, orderId) {
				uni.navigateTo({
					url: "/pages/goods/goods_comment_con/index?unique=" + unique + "&uni=" + orderId
				})
			},
			jumpCon(id) {
				if (this.jump) {
					uni.navigateTo({
						url: `/pages/goods_details/index?id=${id}`
					})
				} else if (this.jumpDetail) {
					uni.navigateTo({
						url: `/pages/goods/order_details/index?order_id=${this.orderId}`
					})
				}
			},
			logistics(order_id) {
				uni.navigateTo({
					url: '/pages/goods/goods_logistics/index?orderId=' + order_id
				})
			},
			confirmOrder(orderId) {
				this.$emit('confirmOrder', orderId)
			},
			changeOperation() {
				this.operationModel = !this.operationModel
			},
			openSubcribe(item) {
				let cartList = [];
				cartList.push({
					cart_id: item.id,
					cart_num: item.surplus_refund_num
				})
				let obj = JSON.stringify(cartList);
				this.$emit('openSubcribe',
					`/pages/goods/goods_return/index?orderId=${this.orderId}&id=${this.oid}&cartIds=${obj}`)
			},
		}
	}
</script>

<style scoped lang="scss">
	.goodWrapper {
		.goods_item {
			position: relative;

			.change_icon {
				position: absolute;
				left: 30rpx;
				top: 0;
				width: 132rpx;
				height: 31rpx;
				border-radius: 8rpx 0px 8rpx 0px;
				background: #383838;
				text-align: center;
				font-size: 20rpx;
				color: #E8CD8E;
			}

		}
	}

	.btm_box {
		margin-top: 10rpx;
		padding: 0 30rpx;

		.count_num {
			font-size: 28rpx;
			color: #2B2B32
		}
	}

	.cart_box {
		height: 54rpx;

		.stock {
			font-size: 20rpx;
			line-height: 54rpx;
			color: #aaa;
		}

		.line {
			padding: 0 6rpx;
		}

		.iconfont {
			font-size: 20rpx;
		}

		view {
			// border: 1px solid #a4a4a4;
			width: 40rpx;
			height: 40rpx;
			text-align: center;
			// line-height: 54rpx;
			color: #282828;
			font-size: 45rpx;
		}

		.mysicle {
			border-radius: 50% !important;
			border: 2rpx solid var(--view-theme);
			overflow: hidden;

			.iconfont {
				color: var(--view-theme);
			}

		}

		.reduce {
			border-radius: 6rpx 0 0 6rpx;
			// line-height: 48rpx;
			font-size: 60rpx;

		}

		.reduce.on {
			// border-color: #e3e3e3;
			border: 2rpx solid #e3e3e3;
			color: #DEDEDE !important;

			.iconfont {
				color: #DEDEDE !important;
			}
		}

		.plus {
			border-radius: 0 6rpx 6rpx 0;
			// line-height: 46rpx;
		}

		.plus.on {
			// border-color: #e3e3e3;
			border: 2rpx solid #e3e3e3;
			color: #dedede !important;

			.iconfont {
				color: #DEDEDE !important;
			}
		}

		.num {
			background: #F6F6F6;
			color: #282828;
			font-size: 28rpx;
			margin: 0 20rpx;
			width: 90rpx;
			border-radius: 4rpx;
			height: 52rpx;
			line-height: 52rpx;
		}
	}

	.gold_price {
		border-radius: 8rpx;
		padding: 0 27rpx;
		color: #383838;
		font-size: 26rpx;
		height: 38rpx;
		line-height: 38rpx;
		background: linear-gradient(90deg, #F2E2AE 0%, #F9F4E5 47.92%, #EDDEB1 100%);
	}

	.price_item {
		font-size: 26rpx;
		color: #737272;
		margin-right: 10rpx;

		text {
			color: #E63523;
		}

		.num {
			color: #171616;
			font-size: 28rpx;
			margin-left: 17rpx;
		}

	}

	.fontcolor {
		color: #e93323;
	}

	.orderGoods {
		background-color: #fff;
	}

	.orderGoods .total {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		// height: 86rpx;
		padding: 0 30rpx;
		border-bottom: 2rpx solid #f0f0f0;
		font-size: 30rpx;
		color: #282828;
		line-height: 86rpx;
		box-sizing: border-box;


	}

	.botton-btn {
		display: flex;
		align-items: right;
		justify-content: flex-end;
		padding: 0rpx 20rpx 20rpx 20rpx;
	}

	.rig-btn {
		display: flex;
		align-items: center;

		.refund {
			font-size: 26rpx;
			color: #e93323;
		}

		.done {
			font-size: 26rpx;
			color: #F19D2F;
		}
	}

	.logistics {
		// height: 46rpx;
		line-height: 35rpx;
		color: #666666;
		font-size: 26rpx;
		border: 1px solid #CCCCCC;
		border-radius: 30rpx;
		padding: 8rpx 25rpx;
		margin-left: 10rpx;
	}

	.sure {
		color: #e93323;
		border: 1px solid #e93323;
	}

	.more-operation {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 10rpx 0;
		color: #bbb;
	}

	.b-top {
		margin-left: 30rpx;
		margin-right: 30rpx;
		border-top: 1px solid #f0f0f0
	}

	.fade-enter-active,
	.fade-leave-active {
		transition: all 0.1s;
	}

	.fade-enter,
	.fade-leave-to

	/* .fade-leave-active below version 2.1.8 */
		{
		opacity: 0;
		transform: translateY(-10px);
	}

	.op {
		opacity: 0.5;

	}

	.gray {
		filter: grayscale(100%);
		filter: gray;
	}

	.pic {
		display: flex;
		justify-content: space-between;
	}

	.valid {
		margin-left: 20rpx;
		font-size: 26rpx;
	}

	.pay-price {
		color: #E93323;
	}

	.refund {
		text-align: right;
		font-size: 26rpx;
		color: var(--view-theme);
	}
</style>